package com.yunhe.policyfulfillment.util;

import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.authority.model.CurrentUser;
import com.yunhe.policyfulfillment.authority.repository.IPolicyOrgRepository;
import com.yunhe.policyfulfillment.authority.repository.IPolicyUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求头工具类
 * @author liuronglei
 */

@Component
public class MyHeaderUtil {

    @Autowired
    private IPolicyUserRepository policyUserRepository;

    @Autowired
    private IPolicyOrgRepository policyOrgRepository;

    public Long getHeaderUserId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        CurrentUser currentUser = (CurrentUser) request.getAttribute("currentUser");
        if (currentUser ==null) {
            return null;
        }
        Long userId = Long.parseLong(currentUser.getUserId());
        return userId;
    }

    public PolicyUser getHeaderUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        CurrentUser currentUser = (CurrentUser) request.getAttribute("currentUser");
        if (currentUser ==null) {
            return null;
        }
        Long userId = Long.parseLong(currentUser.getUserId());
        if (userId == null) {
            return null;
        }
        return policyUserRepository.searchPolicyUserByPrimaryKey(userId);
    }

    public String getUserName(Long publishPerson) {
        return policyUserRepository.getUserName(publishPerson);
    }

    public String getOrgName(Integer orgId) {
        return policyOrgRepository.searchPolicyOrgByPrimaryKey(orgId).getOrgName();
    }
}
